package com.qire.manhua.view.attributeBinder;

import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.qire.common.GlideApp;
import com.qire.common.basic.ObjectUtil;
import com.qire.common.basic.StringUtil;
import com.qire.common.widget.ReferBinder;
import com.qire.common.widget.animation.AnimationBinder;
import com.qire.manhua.view.TileLayout;
import com.qire.manhua.view.attributeBinder.attribute.ImageAttribute;

import androidx.databinding.BindingAdapter;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.ItemDecoration;

/**
 * 原生控件属性扩展绑定适配器
 */
public class ViewAttrAdapter {

    //region    #宽高 begin
    @BindingAdapter("widthAndHeight")
    public static void setWidthAndHeight(View view, int pixels) {
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        if(layoutParams == null) {
            layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        layoutParams.width    = pixels;
        layoutParams.height   = pixels;
        view.setLayoutParams(layoutParams);
    }

    @BindingAdapter("width")
    public static void setWidth(View view, int pixels) {
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        if(layoutParams == null) {
            layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        layoutParams.width    = pixels;
        view.setLayoutParams(layoutParams);
    }

    @BindingAdapter("height")
    public static void setHeight(View view, int pixels) {
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        if(layoutParams == null) {
            layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        layoutParams.height   = pixels;
        view.setLayoutParams(layoutParams);
    }
    //endregion #宽高 end

    //region    #Margin begin
    @BindingAdapter("android:layout_marginTop")
    public static void setMarginTop(View view, int topMargin) {
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        if(layoutParams instanceof ViewGroup.MarginLayoutParams) {
            ViewGroup.MarginLayoutParams marginLayoutParams = (ViewGroup.MarginLayoutParams) layoutParams;
            marginLayoutParams.topMargin = topMargin;
        }
    }
    @BindingAdapter("android:layout_marginBottom")
    public static void setMarginBottom(View view, int bottomMargin) {
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        if(layoutParams instanceof ViewGroup.MarginLayoutParams) {
            ViewGroup.MarginLayoutParams marginLayoutParams = (ViewGroup.MarginLayoutParams) layoutParams;
            marginLayoutParams.bottomMargin = bottomMargin;
        }
    }
    @BindingAdapter("android:layout_marginLeft")
    public static void setMarginLeft(View view, int leftMargin) {
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        if(layoutParams instanceof ViewGroup.MarginLayoutParams) {
            ViewGroup.MarginLayoutParams marginLayoutParams = (ViewGroup.MarginLayoutParams) layoutParams;
            marginLayoutParams.leftMargin = leftMargin;
        }
    }
    @BindingAdapter("android:layout_marginRight")
    public static void setMarginRight(View view, int rightMargin) {
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        if(layoutParams instanceof ViewGroup.MarginLayoutParams) {
            ViewGroup.MarginLayoutParams marginLayoutParams = (ViewGroup.MarginLayoutParams) layoutParams;
            marginLayoutParams.rightMargin = rightMargin;
        }
    }
    //endregion #Margin end

    //region    #drawableIcon begin
    private static Drawable getHaveBoundsDrawable(int drawableRid, Resources resources) {
        Drawable drawable = drawableRid == 0 ? null : resources.getDrawable(drawableRid, null);
        if (ObjectUtil.nonNull(drawable)) {
            drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        }
        return drawable;
    }

    @BindingAdapter({"android:drawableTop"})
    public static void setDrawableTop(TextView view, int drawableRid) {
        Drawable drawable = getHaveBoundsDrawable(drawableRid, view.getResources());
        Drawable[] drawables = view.getCompoundDrawables();
        view.setCompoundDrawables(drawables[0], drawable, drawables[2], drawables[3]);
    }
    @BindingAdapter({"android:drawableBottom"})
    public static void setDrawableBottom(TextView view, int drawableRid) {
        Drawable drawable = getHaveBoundsDrawable(drawableRid, view.getResources());
        Drawable[] drawables = view.getCompoundDrawables();
        view.setCompoundDrawables(drawables[0], drawables[1], drawables[2], drawable);
    }
    @BindingAdapter({"android:drawableLeft"})
    public static void setDrawableLeft(TextView view, int drawableRid) {
        Drawable drawable = getHaveBoundsDrawable(drawableRid, view.getResources());
        Drawable[] drawables = view.getCompoundDrawables();
        view.setCompoundDrawables(drawable, drawables[1], drawables[2], drawables[3]);
    }
    @BindingAdapter({"android:drawableRight"})
    public static void setDrawableRight(TextView view, int drawableRid) {
        Drawable drawable = getHaveBoundsDrawable(drawableRid, view.getResources());
        Drawable[] drawables = view.getCompoundDrawables();
        view.setCompoundDrawables(drawables[0], drawables[1], drawable, drawables[3]);
    }
    //endregion #drawableIcon end

    //region    #text begin
    @BindingAdapter({"android:textSize"})
    public static void setTextSize(TextView view, int size) {
        view.setTextSize(size);
    }

    @BindingAdapter("android:textColor")
    public static void setTextColor(TextView view, String colorStr) {
        if(StringUtil.isEmpty(colorStr)) {
            return;
        }
        ColorStateList colorStateList = ColorStateList.valueOf(Color.parseColor(colorStr));
        view.setTextColor(colorStateList);
    }

    @BindingAdapter("android:textColor")
    public static void setTextColor(TextView view, int resId) {
        view.setTextColor(view.getResources().getColorStateList(resId));
    }

    @BindingAdapter("android:textColor")
    public static void setTextColor(TextView view, ColorStateList colorStateList) {
        if(colorStateList == null) {
            return;
        }
        view.setTextColor(colorStateList);
    }

    @BindingAdapter("android:paintFlags")
    public static void setPaintFlags(TextView view, int paintFlags) {
        // 原来绑定支持改行为的，只是绑定的属性会擦除原有的笔刷标记，固该做自己控制，如果遇到需要重置其他笔刷标记只保留已有的，需要重新实现。
        if(paintFlags >= 0) {
            view.setPaintFlags(view.getPaintFlags() | paintFlags);
        }
    }
    //endregion #text end

    //region    #Image begin

    @BindingAdapter("android:src")
    public static void setSrc(ImageView view, int resId) {
        view.setImageResource(resId);
    }

    @BindingAdapter("android:src")
    public static void setSrc(ImageView view, Bitmap bitmap) {
        if(ObjectUtil.nonNull(bitmap) && !bitmap.isRecycled()) {
            view.setImageBitmap(bitmap);
        } else {
            view.setImageDrawable(null);
        }
    }

    @BindingAdapter("android:src")
    public static void setSrc(ImageView view, String bitmapPath) {
        if(ObjectUtil.nonNull(bitmapPath)) {
            GlideApp.with(view.getContext())
                    .load(bitmapPath)
                    .skipMemoryCache(false) // 是否跳过内存缓存，true 允许， false 不允许，如果允许条过内存缓存，则在 ListView 或 RV这些适配VIEW中加载更多数据引起闪烁
                    .into(view);
        } else {
            view.setImageDrawable(null);
        }
    }

    @BindingAdapter("android:src")
    public static void setSrc(ImageView view, ImageAttribute attribute) {
        if(ObjectUtil.nonNull(attribute)) {
            if(StringUtil.isEmpty(attribute.getUrl())) {
                view.setImageResource(attribute.getRid());
            } else {
                GlideApp.with(view.getContext())
                        .load(attribute.getUrl())
                        .override(attribute.getWidth(), attribute.getHeight())
                        .placeholder(attribute.getPlaceholder())
                        .into(view);
            }
        } else {
            view.setImageDrawable(null);
        }

    }
    //endregion #Image end

    //region    #background begin
    @BindingAdapter("android:background")
    public static void setBackground(View view, int resId) {
        view.setBackgroundResource(resId);
    }

    @BindingAdapter("android:background")
    public static void setBackground(View view, String colorStr) {
        if(StringUtil.isEmpty(colorStr)) {
            return;
        }
        if(colorStr.startsWith("#")) {
            view.setBackgroundColor(Color.parseColor(colorStr));
        }
    }

    @BindingAdapter("backgroundAlpha")
    public static void setBackgroundAlpha(View view, int alpha) {
        //一定不能使用这中方式设置背景透明，这会造成所有白色背景都设置不上变为透明
//        view.getBackground().setAlpha(alpha);
        view.getBackground().mutate().setAlpha(alpha);
    }
    //endregion #background end

    //region    #自定义扩展 begin
    @BindingAdapter("itemDecoration")
    public static void setItemDecoration(RecyclerView recyclerView, ItemDecoration[] itemDecorations) {
        if (itemDecorations == null) {
            return;
        }
        for(ItemDecoration itemDecoration : itemDecorations) {
            recyclerView.removeItemDecoration(itemDecoration);
            recyclerView.addItemDecoration(itemDecoration);
        }
    }

    @BindingAdapter("animation")
    public static void setAnimation(View view, AnimationBinder animationBinder) {
        if(animationBinder == null) {
            return;
        }
        animationBinder.bindView(view);
    }

    @BindingAdapter("referBinder")
    public static void setReferBinder(View view, ReferBinder referBinder) {
        if(referBinder == null) {
            return;
        }
        referBinder.bindView(view);
    }

    @BindingAdapter("layout_lineBreak")
    public static void setLineBreak(View view, boolean isLineBreak) {
        ViewGroup.LayoutParams lp = view.getLayoutParams();
        if(lp instanceof TileLayout.LayoutParams) {
            TileLayout.LayoutParams layoutParams = (TileLayout.LayoutParams) lp;
            layoutParams.isLineBreak = isLineBreak;
            view.requestLayout();
        }
    }
    //endregion #自定义扩展 end

}